package clusteringAlg;

import java.util.List;

import org.opencv.core.Mat;


public class Mean extends Sample{

	private List<RelationSample> Images;
	
	
	/**
	 * @param imageIndexes
	 */
	public Mean(List<RelationSample> imageIndexes) {
		super();
		Images = imageIndexes;
	}
	
	
	/**
	 * 
	 */
	public Mean() {
		super();
		this.Images = null;
	}


	/**
	 * @param image
	 * @param pose
	 * @param array
	 */
	public Mean(Mat image, int pose, double[] array) {
		super(image, pose, array);
		this.Images = null;
	}
	
	/**
	 * @param image
	 * @param pose
	 * @param array
	 * @param images
	 */
	public Mean(Mat image, int pose, double[] array, List<RelationSample> imageIndexes) {
		super(image, pose, array);
		this.Images = imageIndexes;
	}


	/**
	 * @param image
	 * @param pose
	 */
	public Mean(Mat image, int pose) {
		super(image, pose);
		this.Images = null;
	}


	/**
	 * @param image
	 */
	public Mean(Mat image) {
		super(image);
		this.Images = null;
	}
	
	public double CalcMean(){
		
		double meanVal, var = 0, weight=0, temp = 0;
		double[] array = null;
		
		if (this.getScaledImage() == null){
			if (this.getImage() == null){
				System.err.println("Image is not set");
				return -1;
			}else{
				this.ScaleImage(0, 1);
				array = this.getScaledImage();
			}
		}		
		else{
			array = this.getScaledImage();
		}
		
		for (int i=0; i<array.length; i++){
			
			meanVal = 0;
			weight = 0;
			
			for (RelationSample s: this.getImages()){
				
				temp = Math.pow(s.relation, 2.0);
				weight += temp;
				meanVal += s.getScaledImage()[i] * temp;
			}
			
			meanVal /= weight;
			
			var += Math.abs(array[i] - meanVal);
			array[i] = meanVal;
		}
		
		this.setScaledImage(array);
		
		return var;
	}


	public List<RelationSample> getImages() {
		return Images;
	}
	public void setImages(List<RelationSample> imageIndexes) {
		Images = imageIndexes;
	}
	public RelationSample getAtImages(int index) {
		return Images.get(index);
	}
	public void setAtImages(RelationSample value, int index) {
		Images.set(index, value);
	}
	public void addImages(RelationSample value) {
		Images.add(value);
	}
	public int getImagesCount() {
		return Images.size();
	}

}
